package com.spring.common.allocation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

import com.spring.common.redis.JedisManager;
import com.spring.sys.po.SysDic;
import com.spring.sys.service.ISysDicService;
import com.spring.trade.po.TradeServiceRule;
import com.spring.trade.service.ITradeServiceRuleService;
import com.spring.util.constant.Constant;

/**
 * 基础数据初始化
 * @author Administrator
 *
 */
public class BaseDataInitialize  implements ServletContextAware {
	    private final static Logger logger = Logger.getLogger(BaseDataInitialize.class);
	    private ServletContext application;
	    @Autowired
		private ITradeServiceRuleService tradeServiceRuleService;
	    
	    @Autowired
	    private ISysDicService sysDicService;
	    

	   
	    @Override
	    public void setServletContext(ServletContext servletContext) {
	        application = servletContext;
	    }

	    public ServletContext getServletContext() {
	        return this.application;
	    }

	    public void init() throws Exception {
	    	//保存到上下文路径中
	        //getServletContext().setAttribute("xxx","");
	        //规则查询
			List<TradeServiceRule> tradeServiceRuleList= tradeServiceRuleService.findServiceRuleList(null);
			
			
			
			//键：serviceId 值：serviceId 对应的List
			Map<String,List<TradeServiceRule>> map=new HashMap<String,List<TradeServiceRule>>();
			for(TradeServiceRule rule:tradeServiceRuleList){
				String serviceId=String.valueOf(rule.getServiceId());
				List<TradeServiceRule> list=map.get(serviceId);
				if(list==null)
				{
					list=new ArrayList<TradeServiceRule>();
				}
				list.add(rule);
				map.put(serviceId, list);
			}
			
			for(Entry<String, List<TradeServiceRule>> entry:map.entrySet()){ 
				String redisKey=Constant.SERVICE_RULE+"_"+entry.getKey();
			    List<TradeServiceRule> list=entry.getValue(); 
			    Map<String,TradeServiceRule> ruleMap=new HashMap<String,TradeServiceRule>();
			    for(TradeServiceRule rule:list){
			    	String code=rule.getCode();
			    	String key=Constant.SERVICE_RULE_CODE+"_"+code;
			    	ruleMap.put(key, rule);
			    }
			   
			    JedisManager.setObject(redisKey, 0, ruleMap);
			    logger.info("初始化服务规则到Redis成功" +redisKey+"  ---->  "+ruleMap);
	        }  
			
			List<SysDic> sysDicList = sysDicService.findDictByParentCode(Constant.SYS_DIC_CODE);
			//设置定时任务执行时,查询sql的时间间隔,线上是24小时,测试时，做相应的修改,数据库改了之后，要重新启动
			if(!CollectionUtils.isEmpty(sysDicList)){
				SysDic sysDic = sysDicList.get(0);
				
				JedisManager.setObject(Constant.SYS_DIC_CODE, 0, sysDic.getValue());
				
			}
	    }
	    
	    /**
	     * 根据服务id和服务id所对应的serviceCode获取规则
	     * @param serviceId
	     * @param serviceCode
	     * @return
	     */
	    public static Integer getMsgCount(Integer serviceId,String serviceCode){
		    Integer msgCount=null;
			//从Redis取出服务规则
			String redisKey=Constant.SERVICE_RULE+"_"+String.valueOf(serviceId);
			Map<String,TradeServiceRule> ruleMap=(Map<String,TradeServiceRule>)JedisManager.getObject(redisKey);
			if(ruleMap!=null&&ruleMap.size()>0){
				String key=Constant.SERVICE_RULE_CODE+"_"+serviceCode;
				TradeServiceRule rule=ruleMap.get(key);
				if(rule!=null){
					msgCount=Integer.valueOf((rule.getValue()));
				}else{
					logger.error("无法取出服务规则");
				}
			}
			return msgCount;
	    }

		//任务的执行单位,默认是24小时		
	    public static Integer getOrderDateInterval(){
	    	Integer orderDateInterval = null;
	    	orderDateInterval = Integer.valueOf((String)JedisManager.getObject(Constant.SYS_DIC_CODE));
	    	return orderDateInterval;
	    }
}
